﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


class Program
{

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    protected delegate void LogCallBackFuncPtr(int level, IntPtr log);

    private const string moduleName = "StreamCapture";

    [DllImport(moduleName, CallingConvention = CallingConvention.Cdecl)]
    protected static extern void InitializeStreamCapture(LogCallBackFuncPtr cb);

    [DllImport(moduleName, CallingConvention = CallingConvention.Cdecl)]
    public static extern long GetTimestamp();

    [DllImport(moduleName, CallingConvention = CallingConvention.Cdecl)]
    public static extern void TestStruct(AudioPlayParams ap);

    [StructLayout(LayoutKind.Sequential)]
    public struct AudioStream
    {
        public int streamCount;
        public int samplerate;
        public int channels;
        public int channel_layout;
        public int samplefmt;
    }

    [StructLayout(LayoutKind.Sequential)]
    public class AudioPlayParams
    {
        public IntPtr desas;
        public int hwSize;
    };

    protected static LogCallBackFuncPtr logPtr = _LogCallback;

    static void Main(string[] args)
    {
        InitializeStreamCapture(logPtr);
        AudioPlayParams app = new AudioPlayParams();
        AudioStream ap = new AudioStream();
        ap.streamCount = 2;
        ap.samplerate = 44100;
        ap.channels = 3;
        ap.channel_layout = 64;
        ap.samplefmt = 16;
        app.hwSize = 8120;
        int size = Marshal.SizeOf<AudioPlayParams>(app);
        IntPtr intPtr = Marshal.AllocHGlobal(size);
        Marshal.StructureToPtr<AudioStream>(ap, intPtr, true);
        app.desas = intPtr;
        TestStruct(app);
        Marshal.FreeHGlobal(intPtr);
        Console.Read();
    }

    private static void _LogCallback(int level, IntPtr log)
    {
        string msg = Marshal.PtrToStringAnsi(log);
        Console.WriteLine(string.Format("[{0}]:{1}", level, msg));
    }
}

